package com.koicarp.university.graduate.service.service.report.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.koicarp.university.graduate.service.dao.SchoolReportDao;
import com.koicarp.university.graduate.service.dao.SchoolReportStatusDao;
import com.koicarp.university.graduate.service.dto.bigView.StaisfyLeanDto;
import com.koicarp.university.graduate.service.dto.dataCompare.SchoolCountDto;
import com.koicarp.university.graduate.service.entity.report.SchoolReport;
import com.koicarp.university.graduate.service.entity.report.SchoolReportStatus;
import com.koicarp.university.graduate.service.service.report.ISchoolReportService;
import com.koicarp.university.graduate.service.vo.dataCompare.EmployRateVo;
import com.koicarp.university.graduate.service.vo.wx.ReportSchoolVo;
import com.koicarp.university.graudate.common.constant.BaseConstant;
import com.koicarp.university.graudate.common.utils.UniversityResponse;
import com.koicarp.university.graudate.common.utils.UniversityUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 【问卷管理：学校相关问卷表】(SchoolReport)表服务实现类
 *
 * @author liutao
 * @since 2020-11-27 19:27:37
 */
@Slf4j
@Service("schoolReportService")
@RequiredArgsConstructor
public class SchoolReportServiceImpl extends ServiceImpl<SchoolReportDao,SchoolReport> implements ISchoolReportService {

    private final SchoolReportDao schoolReportDao;

    private final SchoolReportStatusDao schoolReportStatusDao;
    private final static HashMap<String,String> leanStar = new HashMap<String, String>(){
        {
            put("1","非常满意");
            put("2","满意");
            put("3","不错");
            put("4","一般");
            put("5","可以接受");
        }
    };
    private final static HashMap<String,String> studyStar = new HashMap<String, String>(){
        {
            put("1","非常能够");
            put("2","比较能够");
            put("3","基本能够");
            put("4","不能够");
            put("5","完全不能够");
        }
    };
    private final static HashMap<String,String> activityStar = new HashMap<String, String>(){
        {
            put("1","专业知识");
            put("2","实验实习");
            put("3","课外科技活动");
            put("4","社会实践");
            put("5","毕业设计");
            put("6","其他");
        }
    };
    private final static HashMap<String,String> classStar = new HashMap<String, String>(){
        {
            put("1","很合理");
            put("2","基本合理");
            put("3","稍有差距");
            put("4","差距很大");
        }
    };

    @Override
    public UniversityResponse getFiveYear() {
        UniversityResponse response = new UniversityResponse();
        response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
        response.message(BaseConstant.RESPONSE_MESSAGE_OPR_SUCCESS);
        String nowYear = UniversityUtil.nowYear();
        Integer year = Integer.parseInt(nowYear);
        Integer[] five = new Integer[5];
        five[4] = year;
        int flag = 4;
        for (int i = 0; i < 5; i++) {
            five[i] = year-flag;
            flag--;
        }
        response.data(five);
        return response;
    }

    @Override
    public UniversityResponse getLeanByNowYear() {
        UniversityResponse response = new UniversityResponse();
        response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
        response.message(BaseConstant.RESPONSE_MESSAGE_OPR_SUCCESS);
        List<StaisfyLeanDto> list = schoolReportDao.selectLeanByNowYear("%"+UniversityUtil.nowYear()+"%");
        for (StaisfyLeanDto s:list){
            s.setName(String.valueOf(leanStar.get(s.getName())));
        }
        response.data(list);
        return response;
    }

    @Override
    public UniversityResponse countStudyByYear(EmployRateVo employRateVo) {
        UniversityResponse response = new UniversityResponse();
        response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
        response.message(BaseConstant.RESPONSE_MESSAGE_OPR_SUCCESS);
        employRateVo.setNowYear("%"+employRateVo.getNowYear()+"%");
        List<SchoolCountDto> res = new ArrayList<>();
        for (int i = 1; i <= 5; i++) {
            SchoolCountDto s  = new SchoolCountDto();
            s.setKey(studyStar.get(String.valueOf(i)));
            s.setValue("0");
            res.add(s);
        }
        List<SchoolCountDto> list = schoolReportDao.countStudyByYear(employRateVo);
        for (SchoolCountDto s: list) {
            s.setKey(studyStar.get(s.getKey()));
            for (SchoolCountDto r: res) {
                if(s.getKey().equals(r.getKey())){
                    r.setValue(s.getValue());
                    break;
                }
            }
        }
        response.data(res);
        return response;
    }

    @Override
    public UniversityResponse countActivityByYear(EmployRateVo employRateVo) {
        UniversityResponse response = new UniversityResponse();
        response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
        response.message(BaseConstant.RESPONSE_MESSAGE_OPR_SUCCESS);
        employRateVo.setNowYear("%"+employRateVo.getNowYear()+"%");
        List<SchoolCountDto> res = new ArrayList<>();
        for (int i = 1; i <= 6; i++) {
            SchoolCountDto s  = new SchoolCountDto();
            s.setKey(activityStar.get(String.valueOf(i)));
            s.setValue("0");
            res.add(s);
        }
        List<SchoolCountDto> list = schoolReportDao.countActivityByYear(employRateVo);
        for (SchoolCountDto s: list) {
            s.setKey(activityStar.get(s.getKey()));
            for (SchoolCountDto r: res) {
                if(s.getKey().equals(r.getKey())){
                    r.setValue(s.getValue());
                    break;
                }
            }
        }
        response.data(res);
        return response;
    }

    @Override
    public UniversityResponse countClassByYear(EmployRateVo employRateVo) {
        UniversityResponse response = new UniversityResponse();
        response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
        response.message(BaseConstant.RESPONSE_MESSAGE_OPR_SUCCESS);
        employRateVo.setNowYear("%"+employRateVo.getNowYear()+"%");
        List<SchoolCountDto> res = new ArrayList<>();
        for (int i = 1; i <= 4; i++) {
            SchoolCountDto s  = new SchoolCountDto();
            s.setKey(classStar.get(String.valueOf(i)));
            s.setValue("0");
            res.add(s);
        }
        List<SchoolCountDto> list = schoolReportDao.countClassByYear(employRateVo);
        for (SchoolCountDto s: list) {
            s.setKey(classStar.get(s.getKey()));
            for (SchoolCountDto r: res) {
                if(s.getKey().equals(r.getKey())){
                    r.setValue(s.getValue());
                    break;
                }
            }
        }
        response.data(res);
        return response;
    }

    @Override
    public UniversityResponse countLeanByYear(EmployRateVo employRateVo) {
        UniversityResponse response = new UniversityResponse();
        response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
        response.message(BaseConstant.RESPONSE_MESSAGE_OPR_SUCCESS);
        employRateVo.setNowYear("%"+employRateVo.getNowYear()+"%");
        List<SchoolCountDto> res = new ArrayList<>();
        for (int i = 1; i <= 5; i++) {
            SchoolCountDto s  = new SchoolCountDto();
            s.setKey(leanStar.get(String.valueOf(i)));
            s.setValue("0");
            res.add(s);
        }
        List<SchoolCountDto> list = schoolReportDao.countLeanByYear(employRateVo);
        for (SchoolCountDto s: list) {
            s.setKey(leanStar.get(s.getKey()));
            for (SchoolCountDto r: res) {
                if(s.getKey().equals(r.getKey())){
                    r.setValue(s.getValue());
                    break;
                }
            }
        }
        response.data(res);
        return response;
    }

    @Override
    @Transactional
    public UniversityResponse saveReportSchoolInfo(ReportSchoolVo reportSchoolVo) {
        UniversityResponse response = new UniversityResponse();
        response.code(BaseConstant.RESPONSE_CODE_SUCCESS_200);
        response.message(BaseConstant.RESPONSE_MESSAGE_OPR_SUCCESS);

        SchoolReport schoolReport = new SchoolReport();
        BeanUtils.copyProperties(reportSchoolVo,schoolReport);
        schoolReportDao.insert(schoolReport);
        SchoolReportStatus schoolReportStatus = new SchoolReportStatus();
        schoolReportStatus.setGraduateId(reportSchoolVo.getGraduateId());
        schoolReportStatus.setSchoolReportId(schoolReport.getId());
        schoolReportStatus.setWriterYear(new Date());
        schoolReportStatusDao.insert(schoolReportStatus);
        return response;
    }


    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SchoolReport queryById(Integer id) {
        return this.schoolReportDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<SchoolReport> queryAllByLimit(int offset, int limit) {
        return this.schoolReportDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param schoolReport 实例对象
     * @return 实例对象
     */
    @Override
    public SchoolReport insert(SchoolReport schoolReport) {
        this.schoolReportDao.insert(schoolReport);
        return schoolReport;
    }

    /**
     * 修改数据
     *
     * @param schoolReport 实例对象
     * @return 实例对象
     */
    @Override
    public SchoolReport update(SchoolReport schoolReport) {
        this.schoolReportDao.update(schoolReport);
        return this.queryById(schoolReport.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.schoolReportDao.deleteById(id) > 0;
    }
}